//
//  M1RealNameViewController.m
//  NewMarryU
//
//  Created by apple on 2018/7/13.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1RealNameViewController.h"
@interface M1RealNameViewController ()<UITextFieldDelegate>
@property (nonatomic ,strong) UITextField *m1_nameTF;
@property (nonatomic ,strong) UITextField *m1_idCardTF;
@property (nonatomic ,strong) UIButton *m1_cerBtn;
@end

@implementation M1RealNameViewController


-(void)m1_cerClick{
    NSDictionary *dic = @{@"id_name":[_m1_nameTF.text m1_removeWhitespace],
                          @"id_card":[_m1_idCardTF.text m1_removeWhitespace]
                          };
    
    [M1HttpNet requestWithMethod:m1_identity_pic_url params:dic success:^(id resDic, NSInteger code, NSString *message) {

        [UIApplication sharedApplication].delegate.window.rootViewController = [M1TabBarController new];
    } fail:^(id resDic, NSInteger code, NSString *message) {
        [M1Alert m1_alertWithTitle:nil message:message buttonTitles:@[@"确定"] action:^(NSInteger index) {
            
        }];

    } showAni:YES];
    
    
}
-(void)m1_back{
    if (_isBack) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    WEAKSELF;
    [M1Alert m1_alertWithTitle:nil message:@"您确定要放弃实名信息的完善吗？未完善的用户不能与Ta人互动。" buttonTitles:@[@"取消",@"确认"] action:^(NSInteger index) {
        if (index == 1) {
            NSArray *m1_vcs = weakSelf.navigationController.viewControllers;
            if ([m1_vcs[0] isKindOfClass:[M1ShuntVC class]]) {
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            }else{
                NSInteger vcindex = 0;
                for (int i=0; i<m1_vcs.count; i++) {
                    if ([m1_vcs[i] isKindOfClass:[M1LoginVC class]]) {
                        vcindex = i;
                        break;
                    }
                }
                [M1User remove];
                [[EMClient sharedClient] logout:YES];
                [weakSelf.navigationController popToViewController:[m1_vcs objectAtIndex:vcindex-1] animated:YES];
            }
        }
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"实名完善";
    [self m1_banPopGesture];
    UIView *m1_bgView1= [[UIView alloc]init];
    m1_bgView1.backgroundColor = [UIColor whiteColor];
    m1_bgView1.layer.cornerRadius = 25;
    m1_bgView1.layer.shadowColor = [UIColor m1_colorHex:0xCAD0DC].CGColor;
    m1_bgView1.layer.shadowOpacity = 0.25;
    m1_bgView1.layer.shadowOffset = CGSizeMake(1, 1);
    [self.view addSubview:m1_bgView1];
    [m1_bgView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(25);
        make.right.offset(-25);
        make.height.mas_offset(50);
        make.top.offset(iPhoneX?139:115);
    }];
    
    _m1_nameTF = [[UITextField alloc]init];
    _m1_nameTF.font = m1_font(16);
    _m1_nameTF.delegate = self;
    _m1_nameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _m1_nameTF.placeholder = @"您的真实姓名";
    _m1_nameTF.tag = 0;
    [_m1_nameTF addTarget:self action:@selector(m1_tfTextChanged:) forControlEvents:UIControlEventAllEditingEvents];
    _m1_nameTF.textColor = [UIColor m1_colorHex:0x333333];
    [m1_bgView1 addSubview:_m1_nameTF];
    
    [_m1_nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(0, 25, 0, 25));
    }];
    
    UIView *m1_bgView2= [[UIView alloc]init];
    m1_bgView2.backgroundColor = [UIColor whiteColor];
    m1_bgView2.layer.cornerRadius = 25;
    m1_bgView2.layer.shadowColor = [UIColor m1_colorHex:0xCAD0DC].CGColor;
    m1_bgView2.layer.shadowOpacity = 0.25;
    m1_bgView2.layer.shadowOffset = CGSizeMake(1, 1);
    [self.view addSubview:m1_bgView2];
    [m1_bgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.right.offset(-20);
        make.height.mas_offset(50);
        make.top.equalTo(m1_bgView1.mas_bottom).offset(20);
    }];
    _m1_idCardTF = [[UITextField alloc]init];
    _m1_idCardTF.font = m1_font(16);
    _m1_idCardTF.placeholder = @"您的身份证号码";
    _m1_idCardTF.delegate = self;
    _m1_idCardTF.tag = 1;
    _m1_idCardTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _m1_idCardTF.textColor = [UIColor m1_colorHex:0x333333];
    [_m1_idCardTF addTarget:self action:@selector(m1_tfTextChanged:) forControlEvents:UIControlEventAllEditingEvents];
    _m1_idCardTF.keyboardType = UIKeyboardTypeASCIICapable;
    [m1_bgView2 addSubview:_m1_idCardTF];
    
    [_m1_idCardTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(0, 20, 0, 20));
    }];
    
    UILabel *m1_LB1 = [[UILabel alloc]init];
    m1_LB1.font = m1_font(12);
    m1_LB1.text= @"*每天最多可验证3次，请认真填写";
    m1_LB1.textColor = [UIColor m1_colorHex:0x999999];
    [self.view addSubview:m1_LB1];
    
    [m1_LB1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(41);
        make.top.equalTo(m1_bgView2.mas_bottom).offset(14);
    }];
    
    _m1_cerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_m1_cerBtn setTitle:@"提交" forState:0];
    [_m1_cerBtn setTitleColor:[UIColor whiteColor] forState:0];
    _m1_cerBtn.titleLabel.font = m1_font(16);
    _m1_cerBtn.layer.cornerRadius = 25;
    _m1_cerBtn.clipsToBounds = YES;
    _m1_cerBtn.userInteractionEnabled = NO;
    [_m1_cerBtn addTarget:self action:@selector(m1_cerClick) forControlEvents:UIControlEventTouchUpInside];
    _m1_cerBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    [self.view addSubview:_m1_cerBtn];
    
    [_m1_cerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.height.mas_offset(50);
        make.width.mas_offset(270);
        make.top.equalTo(m1_LB1.mas_bottom).offset(53);
    }];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([textField isFirstResponder]) {
        if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) {
            [M1Alert m1_alertWithTitle:nil message:@"不支持Emoji表情"];
            return NO;
        }
        if ([string m1_includEmoji]) {
            [M1Alert m1_alertWithTitle:nil message:@"不支持Emoji表情"];
            return NO;
        }
    }
    return YES;
}
-(void)m1_tfTextChanged:(UITextField *)tf{
    if ([_m1_nameTF.text m1_removeWhitespace].length >0&&[_m1_idCardTF.text m1_removeWhitespace].length >0) {
        [_m1_cerBtn setBackgroundImage:[UIImage imageNamed:@"btn_color"] forState:0];
        _m1_cerBtn.userInteractionEnabled = YES;
    }else{
        _m1_cerBtn.userInteractionEnabled = NO;
        [_m1_cerBtn setBackgroundImage:[UIImage new] forState:0];
    }
    
    if (tf.tag == 0) {
        NSString *m1_name = [tf.text m1_removeWhitespace];
        if (m1_name.length >20) {
            _m1_nameTF.text = [m1_name substringWithRange:NSMakeRange(0, 20)];
        }
    }
    if (tf.tag == 1) {
        NSString *m1_card = [tf.text m1_removeWhitespace];
        if (m1_card.length >20) {
            _m1_idCardTF.text = [m1_card substringWithRange:NSMakeRange(0, 20)];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
